<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>


		<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
		<script>
			$(function() {
				var code = getQueryVariable("code");
				var perAuthUrl = getQueryVariable("state");
				if (code == null || code == '') {
					return false;
				} else {
					$.ajax({
						url: 'http://localhost:8880/oauth/token',
						type: 'post',
						dataType: 'json',
						data: {
							client_id: 'webapp',
							client_secret: '123456',
							grant_type: 'authorization_code',
							redirect_uri: 'http://localhost:8848/demo/callback.html',
							code: code
						},
						success: function(data) {
							sessionStorage.setItem("access_token", data.access_token);
							sessionStorage.setItem("username", data.username);
							sessionStorage.setItem("nickname", data.nickname);
							location.href = perAuthUrl;
						}
					})
				}
			})

			function getQueryVariable(variable) {
				var query = window.location.search.substring(1);
				var vars = query.split("&");
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split("=");
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return (false);
			}
		</script>
	</body>
</html>
